import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {
    public static void main(String[] args) {

    }

    @Test
    public void testReadFile1() throws IOException {
        File file = new File("reader.txt");
        FileReader fr = new FileReader(file);
        int data = fr.read();
        //while ((data = fr.read()) != -1)
        while (data != -1) {
            System.out.print((char) data);
            data = fr.read();
        }
        fr.close();
    }
@Test
    public void testReadFile2() {
        FileReader fr = null;
        try {
            File file = new File("reader.txt");
            fr = new FileReader(file);
            int data;
            while ((data = fr.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    @Test
    public void testReadChars() {
        FileReader fr = null;
        try {
            File file = new File("reader.txt");
            fr = new FileReader(file);
            int len;
            char[] buffers = new char[5];
            //返回值是本次读取到的字符数，没有了就是-1
            while ((len = fr.read(buffers)) != -1) {
                for (int i = 0; i < len; i++) {
                    System.out.print(buffers[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
